package monster.com.cvh.util;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import monster.com.cvh.R;

/**
 * Created by ASUS on 2017/12/4.
 */

public class ResgisterButton extends CountDownTimer {
    private TextView mTextView;
    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */
    public ResgisterButton(TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //设置不可点击
        mTextView.setClickable(false);
        //设置到几时
        mTextView.setText(millisUntilFinished / 1000 + "秒后获取");
        mTextView.setBackgroundResource(R.drawable.bg_identify_code_press);
        //获取按钮上的文字
        SpannableString spannableString = new SpannableString(mTextView.getText().toString());

        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);//将倒计时的时间设置为红色
        mTextView.setText(spannableString);
    }

    @Override
    public void onFinish() {
        mTextView.setText("重新获取");
        mTextView.setClickable(true);
        mTextView.setBackgroundResource(R.drawable.bg_identify_code_normal);
    }
}
